package com.example.zhuiwei.springbootbase.config;

import com.example.zhuiwei.springbootbase.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 所有的bean对象都在此处注册
 * WebMvcConfigurerAdapter扩展SpringMVC功能
 */
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/a").setViewName("success");
    }

    /**
     * 所有的 WebMvcConfigurerAdapter组件都会一起起作用
     * @return  WebMvcConfigurerAdapter
     */
    @Bean
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter() {
            //addViewControllers方法是指在controller中return "/home.html"，会自动匹配templates下的dashboard.html
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
//                super.addViewControllers(registry);
                registry.addViewController("/").setViewName("index");
                registry.addViewController("/index.html").setViewName("index");
                registry.addViewController("/home.html").setViewName("dashboard");
            }

            //注册拦截器使LoginHandlerInterceptor生效，springboot已经处理好静态资源的访问了，不用额外处理静态资源如css,js
//            @Override
//        public void addInterceptors(InterceptorRegistry registry) {
////                super.addInterceptors(registry);
//            registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
//                    .excludePathPatterns("/","/index.html","/user/login");
//        }
    };
        return webMvcConfigurerAdapter;
    }

    //使MyLocaleResolver 生效
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}
